package com.xpec.c4.game.datamanager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.annotations.MessageType;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.module.BasicServiceModule;

/**
 * 
 * @author Mike_Tung
 *
 */
public class DataProcessAction {
	private static final Log log = LogFactory.getLog(DataProcessAction.class);
	
	@MessageType(typeCode = MessageTypes.S_DATA_RESPONSE)
	public void dataResponse(DataRequestInfo obj){		
		Contexts.getModule().dataProcessResponsedispatch(obj);
	}
	
	@MessageType(typeCode = MessageTypes.S_CENTRAL_RESPONSE)
	public void centralResponse(CentralAsyncCommand command){	
		
		if(log.isInfoEnabled()){
			log.info(GameUtility.getCurMethodName() + " callBack SN = " + command.getSnCallback());
		}
		
		//獲取callback資料
		CallbackMethodInfo info = Contexts.getModule().getCallbackMethodInfoBySn(command.getSnCallback());
		if (info == null) {
			if(log.isErrorEnabled()){
				log.error(GameUtility.getCurMethodName() + " info = null!!!!");
			}
		}
		
		try {
			if(info != null){
				BasicServiceModule curModule = Contexts.getModule();
				command.setCurrentModuleServerId(curModule.getModuleName());
				command.callBackExecute(info.getUserData());
			}
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(
						GameUtility.getCurMethodName() + " callback execute fail. SN = " + command.getSnCallback(),
						e);
			}
		} finally {
			//清除callback資料
			Contexts.getModule().removeDataProcessCallbackAndUserDataBySn(command.getSnCallback());
		}
		
	}
}
